<template>
  <div>
    <h3>借用未归还查询</h3>
    <el-form :model="form" inline>
      <el-form-item label="员工编号">
        <el-input v-model="form.empno" placeholder="请输入员工编号"></el-input>
      </el-form-item>
      <el-form-item label="飞机注册号">
        <el-select v-model="form.airplane" placeholder="请选择飞机注册号">
          <el-option
            v-for="item in airplane"
            :label="item.airplane"
            :value="item.airplane"
            :key="item.id"
          ></el-option>
        </el-select> </el-form-item
    ></el-form>
    <el-button type="primary" @click="searchList()">查 询</el-button>
    <el-button type="primary" @click="download">下 载</el-button>
    <el-table :data="tableData">
      <el-table-column label="工具名称" prop="toolName"></el-table-column>
      <el-table-column label="工具编号" prop="barcode"></el-table-column>
      <el-table-column label="飞机注册号" prop="airplaneId"></el-table-column>
      <el-table-column label="借用人" prop="borrowByName"></el-table-column>
      <el-table-column label="借用时间" prop="borrowTime"></el-table-column>
      <el-table-column label="借用数量" prop="borrowCount"></el-table-column>
      <el-table-column label="已归还数量" prop="returnCount"></el-table-column>
    </el-table>
    <el-pagination
      background
      layout="prev, pager, next"
      :total="total"
      :current-page="currentPage"
      @current-change="handleCurrentChange"
    ></el-pagination>
  </div>
</template>

<script>
//这里可以导入其他文件（比如：组件，工具 js，第三方插件 js，json 文件，图片文件等等）
//例如：import 《组件名称》 from '《组件路径》';
import airplaneApi from "@/api/airplane/index";
import borrow from "@/api/borrow";
export default {
  //import 引入的组件需要注入到对象中才能使用
  props: {},
  data() {
    //这里存放数据
    return {
      form: {
        empno: "",
        airplane: "",
      },
      airplane: [],
      tableData: [],
      currentPage: 1,
      pageSize: 10,
      total: 0,
    };
  },
  //计算属性 类似于 data 概念
  computed: {},
  //监控 data 中的数据变化
  watch: {},
  //方法集合
  methods: {
    download() {
      if (this.form.empno || this.form.airplane) {
        borrow.download(this.form.empno, this.form.airplane);
      }
    },
    searchList: async function () {
      this.getTotal();
      this.list(1);
    },
    handleCurrentChange: async function (currentPage) {
      this.list(currentPage);
    },
    list: async function (currentPage) {
      if (this.form.empno || this.form.airplane) {
        const res = await borrow.unreturned(
          currentPage,
          this.pageSize,
          this.form.empno,
          this.form.airplane
        );
        this.tableData = res.data;
        this.currentPage = currentPage;
      }
    },
    getTotal: async function () {
      if (this.form.empno || this.form.airplane) {
        const res = await borrow.unreturnedTotal(
          this.form.empno,
          this.form.airplane
        );
        this.total = res.data;
      }
    },
    refreshList: async function (currentPage) {
      await this.getTotal();
      this.list(currentPage);
    },
  },
  //生命周期 - 创建完成（可以访问当前 this 实例）
  created() {
    airplaneApi.all().then((res) => {
      this.airplane = res.data;
    });
    this.refreshList(1);
  },
  //生命周期 - 挂载完成（可以访问 DOM 元素）
  mounted() {},
  beforeCreate() {}, //生命周期 - 创建之前
  beforeMount() {}, //生命周期 - 挂载之前
  beforeUpdate() {}, //生命周期 - 更新之前
  updated() {}, //生命周期 - 更新之后
  beforeDestroy() {}, //生命周期 - 销毁之前
  destroyed() {}, //生命周期 - 销毁完成
  activated() {}, //如果页面有 keep-alive 缓存功能，这个函数会触发
};
</script>
<style scoped>
</style>